package com.anti.modular.bus.basicinformation.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.anti.core.context.login.LoginContextHolder;
import com.anti.jdbc.DBUtils;
import com.anti.jdbc.support.page.PageParam;
import com.anti.jdbc.support.page.PageResult;
import com.anti.commom.core.exception.BusinessException;
import com.anti.modular.bus.basicinformation.entity.BasicInformation;
import com.anti.modular.bus.basicinformation.enums.BasicInformationExceptionEnum;
import com.anti.modular.bus.basicinformation.param.BasicInformationParam;
import com.anti.modular.bus.basicinformation.service.BasicInformationService;
import com.anti.modular.sys.dict.entity.SysDictData;
import com.anti.modular.sys.dict.enums.SysDictTypeExceptionEnum;
import com.anti.modular.sys.dict.service.SysDictDataService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import javax.validation.constraints.NotBlank;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 基础资料service接口实现类
 *
 * @author anti
 * @date 2021-04-07 19:24:48
 */
@Service
public class BasicInformationServiceImpl implements BasicInformationService {
    @Resource
    DBUtils dbUtils;
    @Resource
    SysDictDataService sysDictDataService;

    @Override
    public BasicInformation getById(BasicInformationParam basicInformationParam){
        String sql="select * from bus_room_type where id =? ";
        return dbUtils.get(sql,BasicInformation.class,basicInformationParam.getId());
    }

    @Override
    public BasicInformation getBasicInformationById(Integer id) {
        String sql="select * from bus_room_type where id =? ";
        return dbUtils.get(sql,BasicInformation.class,id);
    }

    @Override
    public PageResult page(PageParam pageParam,BasicInformationParam basicInformationParam) {
        StringBuilder sb=new StringBuilder("select * from bus_room_type where status=0 ");
        ArrayList<Object> param = CollectionUtil.newArrayList();
        if (ObjectUtil.isNotNull(basicInformationParam)) {
            // 根据户型 查询
            if (ObjectUtil.isNotEmpty(basicInformationParam.getName())) {
               sb.append(" and name = ?");
               param.add(basicInformationParam.getName());
            }
        }
        return dbUtils.query(pageParam,sb.toString(),param.toArray());
    }

    @Override
    public List<BasicInformation> list() {
        String sql="select * from bus_room_type where status=0";
        return dbUtils.query(sql,BasicInformation.class);
    }

    @Override
    public void add(BasicInformationParam basicInformationParam) {
        //校验参数，检查是否存在重复的编码或者名称，不排除当前添加的这条记录
        checkParam(basicInformationParam, false);
        BasicInformation basicInformation = new BasicInformation();
        BeanUtil.copyProperties(basicInformationParam, basicInformation);
        basicInformation.setStatus(0);
        basicInformation.setCreateTime(new Date());
        basicInformation.setCreateUser(LoginContextHolder.me().getSysLoginUserId());
        dbUtils.insert(basicInformation);
    }


    public void delete(BasicInformationParam basicInformationParam) {
        BasicInformation basicInformation = this.queryBasicInformation(basicInformationParam);
        basicInformation.setUpdateTime(new Date());
        basicInformation.setUpdateUser(LoginContextHolder.me().getSysLoginUserId());
        basicInformation.setStatus(2);
        dbUtils.update(basicInformation);
    }

    public void edit(BasicInformationParam basicInformationParam) {
        //校验参数，检查是否存在重复的编码或者名称，不排除当前添加的这条记录
        checkParam(basicInformationParam, true);
        BasicInformation basicInformation = this.queryBasicInformation(basicInformationParam);
        BeanUtil.copyProperties(basicInformationParam, basicInformation);
        basicInformation.setUpdateTime(new Date());
        basicInformation.setUpdateUser(LoginContextHolder.me().getSysLoginUserId());
        dbUtils.update(basicInformation);
    }

    @Override
    public BasicInformation detail(BasicInformationParam basicInformationParam) {
        return this.queryBasicInformation(basicInformationParam);
    }

    /**
     * 获取基础资料
     *
     * @author anti
     * @date 2021-04-07 19:24:48
     */
    private BasicInformation queryBasicInformation(BasicInformationParam basicInformationParam) {
        BasicInformation basicInformation = this.getById(basicInformationParam);
        if (ObjectUtil.isNull(basicInformation)) {
            throw new BusinessException(BasicInformationExceptionEnum.NOT_EXIST);
        }
        return basicInformation;
    }

    private void checkParam(BasicInformationParam basicInformationParam, boolean isExcludeSelf){
        Long id = basicInformationParam.getId();
        String bathroom = basicInformationParam.getBathroom();
        String livingroom = basicInformationParam.getLivingroom();
        String bedrooms = basicInformationParam.getBedrooms();
        //构建带name和code的查询条件
        StringBuilder nameSb=new StringBuilder("select count(*) from bus_room_type where status =0 and bedrooms =? and livingroom=? and bathroom=?");
        ArrayList<Object> nameParams = CollectionUtil.newArrayList();
        nameParams.add(bedrooms);
        nameParams.add(livingroom);
        nameParams.add(bathroom);

        //如果排除自己，则增加查询条件主键id不等于本条id
        if (isExcludeSelf) {
            nameSb.append(" and id <> ?");
            nameParams.add(id);
        }

        //查询重复记录的数量
        int countByName = dbUtils.getIntValue(nameSb.toString(),nameParams.toArray());

        //如果存在重复的记录，抛出异常，直接返回前端
        if (countByName >= 1) {
            throw new BusinessException(0,"該戶型已存在");
        }
        SysDictData room_type_bedrooms = sysDictDataService.getDicDataByTypeNameAndDataValue("room_type_bedrooms", basicInformationParam.getBedrooms());
        SysDictData room_type_livingroom = sysDictDataService.getDicDataByTypeNameAndDataValue("room_type_livingroom", basicInformationParam.getLivingroom());
        SysDictData room_type_bathroom = sysDictDataService.getDicDataByTypeNameAndDataValue("room_type_bathroom", basicInformationParam.getBathroom());
        String name=room_type_bedrooms.getValue()+room_type_livingroom.getValue()+room_type_bathroom.getValue();
        basicInformationParam.setName(name);
    }

}
